package com.edgar.baselibrary.network;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import com.android.volley.Request;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.edgar.baselibrary.network.request.IRequest;
import com.edgar.baselibrary.network.request.VolleyDelegateRequest;
import com.edgar.baselibrary.network.response.IResponse;
import com.edgar.baselibrary.network.response.VolleyDelegateResponse;

/**
 * Created by xiexiang on 2015/10/17.
 */
public final class NetworkHelp {

    private RequestQueue mRequestQueue;
    private Context mAppContext;
    private static final NetworkHelp sInstance = new NetworkHelp();
    private final Handler mMainHandler = new Handler(Looper.getMainLooper());
    private NetworkHelp(){}

    public static NetworkHelp getInstance(){
        return sInstance;
    }

    public void init(Context context){
        if(mRequestQueue == null){
            mAppContext = context;
            mRequestQueue = Volley.newRequestQueue(context);
            mRequestQueue.start();
        }
    }

    public RequestQueue getRequestQueue() {
        return mRequestQueue;
    }

    public void cancelRequest(Object tag){
        mRequestQueue.cancelAll(tag);
    }

    /**
     * use the method send get request
     * @param request
     * @param response
     */
    public <T> void sendGetRequest(IRequest<T> request,
                                   IResponse<T> response){
        VolleyDelegateRequest delegateRequest = newRequest(Method.GET,request,response);
        addRequest(delegateRequest);
    }

    /**
     * use the method send post request
     * @param request
     * @param response
     */
    public <T> void sendPostRequest(IRequest<T> request,
                                    IResponse<T> response){
        VolleyDelegateRequest delegateRequest = newRequest(Method.POST,request,response);
        addRequest(delegateRequest);
    }

    /**
     * 建议使用该方法
     * @param request
     * @param response
     */
    public <T> void addRequest(IRequest<T> request,IResponse<T> response){
        sendPostRequest(request, response);
    }

    /**
     * Default send post request,support low version
     * @see #addRequest(IRequest, IResponse)
     * @param request
     * @deprecated
     */
    public <T> void addRequest(IRequest<T> request){
        addRequest(request, request.getResponse());
    }

    private <T> void addRequest(Request<String> request){
        mRequestQueue.add(request);
    }

    /**
     * new network library is request.
     * @return
     */
    private final <T> VolleyDelegateRequest newRequest(int method,IRequest<T> request,
                                                 IResponse<T> response){
        VolleyDelegateResponse delegateResponse = new VolleyDelegateResponse();
        delegateResponse.setClientResponse(response);
        VolleyDelegateRequest delegateRequest = new VolleyDelegateRequest(method,request.getUrl(),
                delegateResponse);
        delegateRequest.setClientRequest(request);
        delegateRequest.setShouldCache(request.getShouldCache());
        delegateRequest.setTag(request.getTag());
        return delegateRequest;
    }

    public void cancelAll(Object tag){
        mRequestQueue.cancelAll(tag);
    }

    public void stop(){
        if(mRequestQueue != null){
            mRequestQueue.stop();
        }
    }

    public Context getContext(){
        return mAppContext;
    }

    public void postResponse(Runnable response){
        mMainHandler.post(response);
    }
}